/**
 * Created by lenovo on 2017/7/20.
 */
let sign = require('../../common/utils/sign.js');
let config = require('../../config');
let WXMember = require('./WXMemberController');

module.exports = {
    showUserCenter: function (req, res) {
        let title = '用户中心';
        WXMember.getMemberInformation(req,res,function (err, code, result) {
            console.log('WXUserCenterController:'+ err)
            console.log('WXUserCenterController:'+ code)
            console.log('WXUserCenterController:'+ result)
            if(code){
                res.render('wx-404', {
                    title: title,
                    errorMessage: err
                });
            } else{
                let memberInfo = result;
                WXMember.checkPhoneNumberById(memberInfo.openid,function (err, code, result) {
                    if (code){
                        if(code == 10001){
                            WXMember.queryConfig(req,function (data) {
                                res.render('wx-register', {
                                    title: '注册',

                                    sdkKey: config.amap.sdkKey,
                                    open_id: memberInfo.openid,
                                    appId: config.app.appid,
                                    sign_pkg: data.jsapi_ticket,
                                    timestamp: data.timestamp,
                                    nonceStr: data.nonceStr,
                                    signature: data.signature,
                                    ak: config.bmap.AK,
                                });
                            });
                        } else {
                            res.render('wx-404', {
                                title: title,
                                errorMessage: err
                            });
                        }
                    } else {
                        WXMember.queryConfig(req,function (data) {
                            res.render('wx-usercenter', {
                                title: title,
                                member: memberInfo,

                                sdkKey: config.amap.sdkKey,
                                open_id: memberInfo.openid,
                                appId: config.app.appid,
                                sign_pkg: data.jsapi_ticket,
                                timestamp: data.timestamp,
                                nonceStr: data.nonceStr,
                                signature: data.signature,
                                ak: config.bmap.AK,
                            });
                        });
                    }
                })
            }
        });
    }
};
